package com.micai.springboot;

import net.sf.json.JSONObject;
import org.apache.commons.collections.map.HashedMap;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @FileName: UserBeanSolrTest
 * @Author: zhaoxinguo
 * @Date: 2018/12/28 13:23
 * @Description: sorl client 测试
 */
public class UserBeanSolrTest {

    private static SolrJavaIntegration solrJavaIntegration = null;

    static {
        if (solrJavaIntegration == null) {
            solrJavaIntegration = new SolrJavaIntegration("http://localhost:6789/solr/mote");
        }
    }

    public static void add() {
        List<UserBean> userBeens = new ArrayList<>();
        // add
        UserBean userBean = new UserBean();
        userBean.setId("9");
        userBean.setName("找实习");
        userBean.setStore("1");
        userBean.setNum("32");
        userBean.setImage("0");
        int num = 0;//初始化
        int i = num + 34;
        userBean.setCount(i);
        int j = i + 36;
        userBean.setCount(j);
        userBeens.add(userBean);

        userBeens.forEach(userBean1 -> {
            try {
                solrJavaIntegration.addSolrDocument(userBean);
            } catch (SolrServerException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
    }

    public static void getById() {
        // getById
        try {
            SolrDocument solrDocument = solrJavaIntegration.getSolrClient().getById("1");
            System.out.println(com.alibaba.fastjson.JSONObject.toJSON(solrDocument));
        } catch (SolrServerException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void query() {
        // query
        try {
            SolrQuery query = new SolrQuery();
            query.setQuery("*:*");

            // 多域值排序方式
            SolrQuery.ORDER order = SolrQuery.ORDER.desc;
            String storeOrderName = "store";
            String numOrderName = "num";
            String imageOrderName = "image";
            List<SolrQuery.SortClause> sortClauses = new ArrayList<>();
            sortClauses.add(SolrQuery.SortClause.create(storeOrderName, order));
            sortClauses.add(SolrQuery.SortClause.create(numOrderName, order));
            sortClauses.add(SolrQuery.SortClause.create(imageOrderName, order));
            query.setSorts(sortClauses);

            query.set("sort", "image desc");

            // 单域值排序方式
            /*query.set("sort", "store desc, num desc, image desc");*/

            QueryResponse response = solrJavaIntegration.getSolrClient().query(query);
            SolrDocumentList solrDocumentList = response.getResults();
            if (solrDocumentList.size() > 0) {
                List<UserBean> userBeanList = new ArrayList<>();
                for (SolrDocument solrDocument : solrDocumentList) {
                    JSONObject jsonObject = JSONObject.fromObject(solrDocument);
                    UserBean userBean = (UserBean)JSONObject.toBean(jsonObject, UserBean.class);
                    userBeanList.add(userBean);
                }
                Map<String, Object> resultMap = new HashedMap();
                resultMap.put("data", userBeanList);
                System.out.println(com.alibaba.fastjson.JSONObject.toJSON(resultMap));
            }
        } catch (SolrServerException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String [] args) {
        add();
//        query();
    }

}
